/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tdtest.controls;

import java.util.HashMap;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import res.ImageLoader;
import tdtest.action.AddGroupItem;
import tdtest.action.RemoveGroupItem;
import tdtest.data.DataObject;
import tdtest.data.TDGroup;

/**
 *
 * @author sutter
 */
public class ControlGroup<T extends DataObject> extends SystemComponentControl<TDGroup<T>> {
	
	private GridPane contentList;
	private Button addButton;
	private Image deleteImage;
	private ControlFactory<T> controlFactory; 
	
	public ControlGroup(TDGroup<T> group, ControlFactory<T> controlFactory) {
		super(group);
		getStyleClass().add("control-group");
		this.controlFactory = controlFactory;
		init(group);
	}
	
	@Override
	public void onUpdate() {
		super.onUpdate();
		
		//remove all control objects and add to a map
		HashMap<DataObject,Node> controlObjects = new HashMap<>();
		for(Node node:contentList.getChildren()) {
			if(node instanceof DataControl) {
				controlObjects.put(((DataControl)node).getData(), node);
			}
		}
		contentList.getChildren().clear();
		
		//cycle through data objects and insert, creating if needed
		TDGroup<T> group = this.getData();
		int index = 0;
		for(T data:group.groupItems) {
			Node node = controlObjects.get(data);
			if(node == null) {
				node = createListNode(data);
			}
			insertListNode(data,node,index++);
		}
		
		//discard missing items.
	}
	
	private void init(TDGroup<T> group) {
		addButton = new Button();
		addButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override public void handle(ActionEvent e) {
				AddGroupItem<T> action = new AddGroupItem<>(ControlGroup.this.getData(),controlFactory);
				action.run();
			}
		});
		Image addImage = ImageLoader.getImage("add.png");
		addButton.setGraphic(new ImageView(addImage));
		addButton.getStyleClass().add("add-button");
		addButton.getStyleClass().add("image-button");
		getNameSuffixPane().getChildren().add(addButton);
		
		contentList = new GridPane();
		ColumnConstraints column0 = new ColumnConstraints();
		contentList.getColumnConstraints().addAll(column0);
		ColumnConstraints column1 = new ColumnConstraints(10,10,Double.MAX_VALUE);
		column1.setHgrow(Priority.ALWAYS);
		contentList.getColumnConstraints().addAll(column1);
		contentList.getStyleClass().add("group-entry-pane");
		
		deleteImage = ImageLoader.getImage("delete.png");
		int index = 0;
		for(T item:group.groupItems) {
			Node node = createListNode(item);
			insertListNode(item,node,index++);
		}
		
		this.setContent(contentList);

	}
	
	private Node createListNode(T data) {
		return controlFactory.createControl(data);
	}
	
	private void insertListNode(final T data, Node node, int index) {
		VBox vb = new VBox();
		Button button = new Button();
		button.setOnAction(new EventHandler<ActionEvent>() {
			@Override public void handle(ActionEvent e) {
				RemoveGroupItem<T> action = new RemoveGroupItem<>(ControlGroup.this.getData(),data);
				action.run();
			}
		});
		button.setGraphic(new ImageView(deleteImage));
		button.getStyleClass().add("close-button");
		button.getStyleClass().add("image-button");
		vb.getChildren().add(button);
		vb.setAlignment(Pos.TOP_LEFT);
		contentList.add(vb,0,index);
		contentList.add(node,1,index);
	}
}
